//
//  LZXHttpRequest.m
//  SNSDemo
//
//  Created by LZXuan on 15-4-2.
//  Copyright (c) 2015年 LZXuan. All rights reserved.
//

#import "LZXHttpRequest.h"
@interface LZXHttpRequest()

//block  要copy
@property (nonatomic,copy) DownloadBlock successBlock;
@property (nonatomic,copy) FailBlock failBlock;
@end

@implementation LZXHttpRequest
- (void)dealloc {
    self.successBlock = nil;
    self.failBlock = nil;
    
    [_downloadData release],_downloadData = nil;
    //self.downloadData = nil;
    [super dealloc];
}
- (instancetype)init {
    if (self = [super init]) {
        _downloadData = [[NSMutableData alloc] init];
    }
    return self;
}
/**
 *  下载请求连接
 *
 *  @param urlStr 网址
 */
//http:一个请求 只能 连接一次  下载完成之后 连接 断开
//如果要再下载 那么需要 重新 请求连接
- (void)downloadRequestWithUrl:(NSString *)urlStr success:(DownloadBlock)successBlock fail:(FailBlock)failBlock{
    //保存block
    self.successBlock = successBlock;
    self.failBlock = failBlock;
    
    
    //创建 请求
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:urlStr]];
   
    if (_httpRequest) {
        //看有没有保存之前的请求
        [_httpRequest release];
        _httpRequest = nil;
    }
    //创建请求连接
    _httpRequest = [[NSURLConnection alloc] initWithRequest:request delegate:self];
}

#pragma mark - post 
//post
/*
 http://223.6.252.214/weibofun/add_count.php?apiver=10500&type=like&category=%@&fid=%@
 */
- (void)postRequestWithUrl:(NSString *)urlStr params:(NSString *)paramStr success:(DownloadBlock)successBlock fail:(FailBlock)failBlock {
    if (_httpRequest) {
        [_httpRequest release];
        _httpRequest = nil;
    }
    self.successBlock = successBlock;
    self.failBlock = failBlock;
    
    //url地址
    //http://223.6.252.214/weibofun/add_count.php?apiver=10500
    //请求体拼接参数
    //type=like&category=weibo_jokes&fid=123
    
    //分理出 参数 和 服务器地址
    //把参数 转化为NSData 作为请求体进行上传 提交数据
    
    //创建 可变的请求
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:urlStr]];
    request.HTTPMethod = @"POST";
    
    
    //把参数 转化为NSData  作为请求体
    NSData *data = [paramStr dataUsingEncoding:NSUTF8StringEncoding];
    request.HTTPBody = data;
    //请求头
    [request addValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
    [request addValue:[NSString stringWithFormat:@"%d",data.length] forHTTPHeaderField:@"Content-Length"];
    
    _httpRequest = [[NSURLConnection alloc] initWithRequest:request delegate:self];
}

#pragma mark - 协议的方法
//客户端 先 收到 服务器的响应 告知客户端 马上发数据
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
    [self.downloadData setLength:0];//清空原来的数据
}
//客户端 收数据
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
    [self.downloadData appendData:data];
}
//下载完成 服务器发送数据完成
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
    //下载完成 之后 当前对象 只管下载 不管处理，这时可以委托 别人处理
    //当前类的对象 是主动方
    
    //委托关系 就可以用代理设计模式 1.协议规范代理  2. target -action 3.block回调
    
    //调用block  给block 传 当前对象地址
    if (self.successBlock) {
        self.successBlock(self);
    }
    
}

//下载异常
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
    NSLog(@"网络有异常");
    if (self.failBlock) {
        self.failBlock(error);
    }
}


@end
